package day06;

import sun.security.util.Length;

import java.util.Arrays;
import java.util.Random;

//方法的演示
public class MethodDemo {
    public static void main(String[] args) {
        /*
        say();  //调用say()方法
        System.out.println("-----------------");

        //sayHi(); //编译错误, 有参则必须传参
        //sayHi(250); //编译错误, 参数类型必须匹配
        sayHi("zhangsan"); //String name = "zhangsan"
        sayHi("lisi"); //String name = "lisi"
        System.out.println("-------------------");

        sayHello("zhangsan", 25); //String name = "zhangsan", int age = 25
        sayHello("lisi", 27); //String name = "lisi", int age = 27
        System.out.println("------------------------");
         */

        say(); //自动绑定无参say
        say("zhangsan"); //自动绑定String参的say
        say("lisi", 25); //自动绑定String + int参的say
        //say(3.146); //编译错误, 没有double参的say
        System.out.println("---------------------------");


        int a = sum(5, 6); //sum(5, 6)的值就是return后的那个数
        System.out.println(a); //11----模拟对返回值的后续操作
        System.out.println("--------------------------");

        int m = 5, n = 6;
        int b = sum(m, n); //传的是m和n里面的数
        System.out.println(b); //11----模拟对返回值的后续操作
        System.out.println("-------------------------");

        int[] c = generateArray(5, 10); //模拟第1个人的访问
        System.out.println("数组的长度:" + c.length); //----模拟对返回值的后续操作
        for (int i = 0; i < c.length; i++) {
            System.out.println(c[i]);
        }
        System.out.println("---------------------------");

        int[] d = generateArray(10, 30); //模拟第2个人的访问
        System.out.println("第1个元素的值:" + d[0]); //----模拟对返回值的后续操作
        Arrays.sort(d); //---模拟对返回值的后续操作
        for (int i = 0; i < d.length; i++) {
            System.out.println(d[i]);
        }
    }

    //无参无返回值
    public static void say() {
        System.out.println("大家好, 我叫WKJ, 今年40岁了");
    }

    //有参无返回值
    public static void say(String name) {
        System.out.println("大家好, 我叫" + name + ", 今年40岁了");
    }

    //有参无返回值
    public static void say(String name, int age) {

        System.out.println("大家好, 我叫" + name + ", 今年" + age + "岁了");
    }

    public static void say(int age) {} //正确的重载
    public static void say(int age, String name) {} //正确的重载
    //public static int say() {  return 1;  } //编译错误, 重载与返回值类型无关
    //public static void say(String address) {} //编译错误, 重载与参数名称无关

    //有参有返回值
    public static int sum(int num1, int num2) {
        int num = num1 + num2;
        return num; //返回的是num里面的那个数
        //return num1 + num2; //返回的是num1与num2的和
    }

    //生成一个整型数组, 并填充随机数据
    public static int[] generateArray(int len, int max) {
        Random rand = new Random();
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = rand.nextInt(max + 1); //包括max
        }
        return arr;
    }

}
